package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.SpecificationDao;
import io.renren.modules.seller.dao.SpecificationOptionDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.SpecificationEntity;
import io.renren.modules.seller.entity.SpecificationOptionEntity;
import io.renren.modules.seller.group.Specification;
import io.renren.modules.seller.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Slf4j
@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Autowired
    private SpecificationDao specificationDao;
    @Autowired
    private SpecificationOptionDao specificationOptionDao;
//    @Autowired
//    private SpecificationOptionService optionService;

    @Override
    public List<SpecificationEntity> findAll() {
        return this.list();
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        String specName = (String) params.get("specName");
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>().
                        like(StringUtils.isNotBlank(specName), "spec_name", specName)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Specification specification) {
        //获取规格实体
        SpecificationEntity tbspecification = specification.getSpecification();
        specificationDao.insert(tbspecification);

        //获取规格选项集合
        List<SpecificationOptionEntity> specificationOptionList = specification.getSpecificationOptionList();
        for (SpecificationOptionEntity option : specificationOptionList) {
            //设置规格ID
            option.setSpecId(tbspecification.getId());
            //新增规格
            specificationOptionDao.insert(option);
        }
//        optionService.saveBatch(specificationOptionList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Specification specification) {
        //获取规格实体
        SpecificationEntity tbspecification = specification.getSpecification();
        specificationDao.updateById(tbspecification);

        //删除原来规格对应的规格选项
        Wrapper<SpecificationOptionEntity> wrapper =
                new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", tbspecification.getId());
        specificationOptionDao.delete(wrapper);

        //获取规格选项集合
        List<SpecificationOptionEntity> specificationOptionList = specification.getSpecificationOptionList();
        for (SpecificationOptionEntity option : specificationOptionList) {
            //设置规格ID
            option.setSpecId(tbspecification.getId());
            //新增规格
            specificationOptionDao.insert(option);
        }
    }

    @Override
    public Specification findOne(Long id) {
        Specification specification = new Specification();
        //获取规格实体
        SpecificationEntity tbSpecification = specificationDao.selectById(id);
        specification.setSpecification(tbSpecification);

        //获取规格选项列表
        QueryWrapper<SpecificationOptionEntity> wrapper =
                new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", tbSpecification.getId());
        List<SpecificationOptionEntity> specificationOptionList = specificationOptionDao.selectList(wrapper);

        specification.setSpecificationOptionList(specificationOptionList);
        //组合实体类
        return specification;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        List<Long> asList = Arrays.asList(ids);
        log.info("spec delete ids:{}", asList);
        this.removeByIds(asList);
        Wrapper<SpecificationOptionEntity> wrapper =
                new QueryWrapper<SpecificationOptionEntity>().in("spec_id", asList);
        specificationOptionDao.delete(wrapper);
    }

    @Override
    public List<Map> selectOptionList() {
        return specificationDao.selectOptionList();
    }
}
